<template>
  <div>
    <el-dialog
      width="550px"
      :title="type == 'add' ? $t('新增') : $t('修改')"
      destroy-on-close
      append-to-body
      :visible.sync="show"
      :before-close="cancel"
    >
      <el-form @submit.native.prevent ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item :label="$t('区域名称')" prop="areaName">
          <el-input v-model="form.areaName" :placeholder="$t('请输入')" />
        </el-form-item>
        <el-form-item :label="$t('标识号A~Z')" prop="areaMarkers">
          <el-select
            :disabled="type == 'edit'"
            style="width: 100%"
            v-model="form.areaMarkers"
            :placeholder="$t('请选择')"
          >
            <el-option v-for="marker in markers" :key="marker" :label="marker.label" :value="marker.value" />
          </el-select>
        </el-form-item>
        <!-- <el-form-item :label="$t('备注')" prop="remark">
          <el-input v-model="form.remark" type="textarea" :rows="3" :placeholder="$t('请输入')" />
        </el-form-item> -->
      </el-form>
      <div slot="footer">
        <el-button @click="handleSubmit" type="primary">{{ $t('确认') }}</el-button>
        <el-button @click="cancel">{{ $t('关闭') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { updateStockPositionArea, addStockPositionArea } from '@/api/distribution/sku/inventoryLocationlList'
export default {
  props: {
    /**
     * type
     */
    type: {
      type: String,
    },
    /**
     * formData
     */
    formData: {
      type: Object,
      default: () => ({}),
    },
    /**
     * 控制显示隐藏
     */
    show: {
      type: Boolean,
      required: true,
      default: false,
    },
  },
  data() {
    return {
      markers: 'A,B,C,D,E,F,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z'.split(',').map((i) => {
        return { lable: i, value: i }
      }),
      // 表单参数
      form: {
        /**区域名称 */
        areaName: '',
        /**标识号 A~Z */
        areaMarkers: '',
        // remark: '',
      },
      // 表单校验
      rules: {
        areaName: [{ required: true, message: this.$t('请输入'), trigger: 'blur' }],
        areaMarkers: [{ required: true, message: this.$t('请输入'), trigger: 'blur' }],
        // remark: [{ required: true, message: this.$t('请输入'), trigger: 'blur' }],
      },
    }
  },
  computed: {},
  watch: {
    show: function (newValue) {
      if (newValue) {
        Object.keys(this.form).forEach((key) => (this.form[key] = ''))
        if (this.type == 'edit') {
          Object.keys(this.formData).forEach((key) => (this.form[key] = this.formData[key] || ''))
        }
      }
    },
  },
  methods: {
    getList() {},

    handleSubmit() {
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
          if (this.type == 'add') {
            const data = {
              ...this.form,
              id: null,
            }
            addStockPositionArea(data).then((res) => {
              const { code, data, msg } = res
              if (code === 200) {
                this.$message({ type: 'success', message: msg })
                this.cancel()
                this.$emit('getList')
              }
            })
          }
          if (this.type == 'edit') {
            const data = {
              ...this.form,
            }
            updateStockPositionArea(data).then((res) => {
              const { code, data, msg } = res
              if (code === 200) {
                this.$message({ type: 'success', message: msg })
                this.cancel()
                this.$emit('getList')
              }
            })
          }
        }
      })
    },
    cancel() {
      this.$emit('update:show', false)
    },
  },
}
</script>
<style scoped lang="scss"></style>
